import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import router from './router'
import * as echarts from 'echarts'
import axios from 'axios'

Vue.prototype.$echarts = echarts
Vue.config.productionTip = false
Vue.use(VueRouter)
Vue.use(ElementUI)


const axiosInstance = axios.create({
  timeout: 10000,
})
// axios前置拦截器
axiosInstance.interceptors.request.use(config => {
  //在所有请求中加入认证token
  let token = window.sessionStorage.getItem('token')
  if (token != null) {
    config.headers['Authorization'] = token
  }
  return config
}, error => {
  return Promise.reject(error)
})
// 响应拦截器(后置拦截器)进行全局错误处理
axiosInstance.interceptors.response.use(response => {
  return response
}, error => {
  const rs = error.response.data
  if (error.response && error.response.status === 401) {
    // 对于401错误，不打印错误信息
    alert(rs.msg);
    router.push('/login').catch(err => {
      console.log('NavigationDuplicated error caught:', err);
    });
    return;
  }
  // 打印其他错误信息
  console.error('Error:', error);
  return Promise.reject(error);
});
Vue.prototype.axios = axiosInstance

new Vue({
  router,
  render: h => h(App),
}).$mount('#app')
